<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>欢迎注册</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../css/style.css">
    <!--    引入vue.js-->
    <script type="text/javascript" src="../js/vue.js"></script>
</head>

<body>

<div class="form-div" id="app">
    <div class="reg-content">
        <h1>欢迎注册</h1>
        <span>已有帐号？</span> <a href="login.html">登录</a>
    </div>
    <el-form id="reg-form" action="/brand-case/registerServlet" method="post">
        <table>
            <tr>
                <td>用户名</td>
                <td class="inputs">
                    <el-input v-model="username" placeholder="请输入用户名"></el-input>
                    <br>
                    <span id="username_err" class="err_msg">{{register_msg}}</span>
                </td>
            </tr>
            <tr>
                <td>密码</td>
                <td class="inputs">
                    <el-input v-model="password" type="password" id="password" placeholder="请输入密码"></el-input>
                    <br>
                    <span id="password_err" class="err_msg" style="display: none">密码格式有误</span>
                </td>
            </tr>
            <tr>
                <td>验证码</td>
                <td class="inputs">
                    <el-input name="checkCode" type="text" v-model="code"></el-input>
                    <img ref="img" id="checkCodeImg" src="http://localhost:8080/users/get" alt="验证码"
                         @click="getCaptchaCode()"></img>
                    <a href="#" id="changeImg">看不清？</a>
                </td>
            </tr>
        </table>
        <div class="buttons">
            <el-button @click="register()">注册</el-button>
        </div>
        <br class="clear">
    </el-form>
</div>
</body>

<style>
    * {
        margin: 0;
        botton: 0;
    }

    body {
        width: 100%;
        height: 100%;
    }

    #app {
        position: relative;
        width: 100%;
        height: 100%;
    }

    .reg-content {
        position: absolute;
        left: 50%;
        transform: translate(-50%, 0);
        height: 100px;
    }

    #reg-form {
        position: absolute;
        left: 50%;
        top: 100px;
        transform: translate(-50%, 0);
    }

    el-from {
        position: relative;
    }

    .buttons {
        position: absolute;
        display: inline-block;
        right: 10px;
    }
</style>
<!-- 引入组件库 -->
<script type="text/javascript" src="../js/index.js"></script>
<script type="text/javascript" src="../js/axios.js"></script>
<script>
    var vm = new Vue({
        el: '#app',
        data() {
            return {
                username: '',
                password: '',
                register_msg: '',
                code: ''
            }
        },
        created() {
            var _this = this;
            _this.getCaptchaCode()
        },
        methods: {
            register() {
                var params = new URLSearchParams()
                params.append("password", this.password)
                params.append("username", this.username)
                params.append("code", this.code)
                axios({
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    data: params,
                    method: 'post',
                    url: 'http://localhost:8080/users/register'
                }).then(function (response) {
                    console.log(response)
                }).catch(function (error) {
                    console.log(error)
                })
            },
            getCaptchaCode() {
                this.$refs.img.src = 'localhost:8080/users/get' + Math.random()
            }
        }
    })
</script>

</html>
